package com.alkalinesolutions.json.compat;

import java.util.Iterator;
import java.util.NoSuchElementException;

import org.json.JSONArray;

/** Helper class, as JSONArray does not expose an iterator
 * 
 * @author David Waite &lt;<a href='mailto:david@alkaline-solutions.com'>david@alkaline-solutions.com</a>&gt;
 *
 */
public class JSONArrayIterator implements Iterator<Object> {

	private JSONArray array;
	private int index = 0;
	private int length;
	
	public JSONArrayIterator(JSONArray array) {
		this.array = array;
		length = array.length();
	}
	
	public boolean hasNext() {
		return index < length;
	}

	public Object next() {
		try {
			return array.opt(index++);
		} catch (ArrayIndexOutOfBoundsException e) {
			index = length;
			throw new NoSuchElementException();
		}
	}

	public void remove() {
		throw new UnsupportedOperationException();
	}

}
